﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Medical_Store_Billing.Business_Login_Layer;

namespace Medical_Store_Billing
{
    public partial class Formquanlykhachhang : Form
    {
        KhachhangBLL m_khachhangbll;
        int flag;
        public Formquanlykhachhang()
        {
            InitializeComponent();
        }
        private void Formquanlykhachhang_Load(object sender, EventArgs e)
        {
            m_khachhangbll = new KhachhangBLL();
            dataGridView1.DataSource = m_khachhangbll.Lay();
            Default();
            btnLuu.Enabled = false;
            btnSua.Enabled = true;
            btnThem.Enabled = true;
        }
    

       
        private void Default()
        {
            txtmakh.Enabled = false;
            txtdiachi.Enabled = false;
            txtdothanthiet.Enabled = false;
            txttenkh.Enabled = false;
            txtsodt.Enabled = false;

        }

        private void Add()
        {
            txtmakh.Enabled = true;
            txtdiachi.Enabled = true;
            txtdothanthiet.Enabled = true;
            txttenkh.Enabled = true;
            txtsodt.Enabled = true;

        }
        private void Edit()
        {
            txtmakh.Enabled = false;
            txtdiachi.Enabled = true;
            txtdothanthiet.Enabled = true;
            txttenkh.Enabled = true;
            txtsodt.Enabled = true;

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            flag =1;
            Add();
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnLuu.Enabled = true;
            txtdiachi.Text = "";
            txtdothanthiet.Text = "";
            txtmakh.Text = "";
            txtsodt.Text = "";
            txttenkh.Text="";
               
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int dong = e.RowIndex;

            txtmakh.Text = dataGridView1.Rows[dong].Cells[0].Value.ToString();
            txttenkh.Text = dataGridView1.Rows[dong].Cells[1].Value.ToString();
            txtdiachi.Text = dataGridView1.Rows[dong].Cells[2].Value.ToString();
            txtsodt.Text = dataGridView1.Rows[dong].Cells[3].Value.ToString();
            txtdothanthiet.Text = dataGridView1.Rows[dong].Cells[4].Value.ToString();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            flag = 2;
            Edit();
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnLuu.Enabled = true;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            m_khachhangbll = new KhachhangBLL();
            if (MessageBox.Show("bạn có muốn lưu?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (CheckNull() == true)
                {
                    if (flag == 1)
                    {
                        if (m_khachhangbll.Themkh(txtmakh.Text,txttenkh.Text,txtdiachi.Text,txtsodt.Text,Int32.Parse(txtdothanthiet.Text)))
                            Formquanlykhachhang_Load(sender,e);
                    }
                    if (flag == 2)
                    {
                        if (m_khachhangbll.Suakh(txtmakh.Text,txttenkh.Text,txtdiachi.Text,txtsodt.Text,Int32.Parse(txtdothanthiet.Text)))
                            Formquanlykhachhang_Load(sender,e);
                    }
                }
                else
                {
                    Formquanlykhachhang_Load(sender,e);
                }
            }
            else
                Formquanlykhachhang_Load(sender,e);
        }
         private bool CheckNull()
        {
            if (txtdiachi.Text == "")
            {
                MessageBox.Show("Địa Chỉ ko đc rỗng");
                return false;
            }
            else if (txtdothanthiet.Text == "")
            {
                MessageBox.Show("Độ thân thiết ko đc rỗng");
                return false;
            }
            else if (txtmakh.Text == "")
            {
                MessageBox.Show("mã khách hàng ko đc rỗng");
                return false;
            }
            else if (txtsodt.Text == "")
            {
                MessageBox.Show("số đt ko đc rỗng");
                return false;
            }
            else if (txttenkh.Text == "")
            {
                MessageBox.Show("tên khách ko đc rỗng");
                return false;
            }
            else return true;
        }

         private void btntimkiem_Click(object sender, EventArgs e)
         {
             m_khachhangbll = new KhachhangBLL();
             dataGridView1.DataSource = m_khachhangbll.Timkh(txttimkiem.Text);
         }

       

       

        
    }
}
